import {Component, OnInit, ViewEncapsulation, Input, Output, EventEmitter} from '@angular/core';
import {Subject} from "rxjs";
import {ToolsService} from "@shared/service/tools/tools.service";
import {EventsService} from "@core/events/events.service";
import {Router} from "@angular/router";

@Component({
  selector: 'seed-goods-ticket-purchase',
  templateUrl: './goods-ticket-purchase.component.html',
  styleUrls: ['./goods-ticket-purchase.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class GoodsTicketPurchaseComponent implements OnInit {
  public params = [];
  public ticketCardInfo; // 票务信息
  public price;
  public playTime;
  public position;
  public current: number;
  public buyTimesLimit: number; // 限购件数,0是无限购买，X是限购件数
  public buyStatus;
  string; // 购买状态 0：不可购买，1：可购买,
  public isDisabled = false;
  public num = 1;   // 购买数量,默认1
  public stock: number;
  public ticketNum: number; // 购买数量
  public buttonColor = false;
  public maxLength: number;
  @Input() tickets;


  constructor(
    private tools: ToolsService,
    private events: EventsService,
    private router: Router,
  ) {
  }

  /**
   * @description 确认按钮
   * @author @zhoukf1349
   * @date 2018-04-04
   * @param val
   */
  handleTicket() {
    if (!this.ticketNum) {
      this.isDisabled = true;
      this.router.navigate(['/goods/p/ticket-order'],
        {queryParams: {skuCode: this.ticketCardInfo.skuCode, num: 1}});
    } else {
      this.isDisabled = true;
      this.router.navigate(['/goods/p/ticket-order'],
        {queryParams: {skuCode: this.ticketCardInfo.skuCode, num: this.ticketNum}});
    }
  }

  /**
   * @description 选择哪个票务，就显示那个票务的信息,buyStatus为0是无效购买，1是有效购买
   * @author @zhoukf1349
   * @date 2018-04-04
   * @param val
   */
  ticketSelectedItem(item, idx) {
    this.ticketNum = 1;
    if (item.buyStatus === 0) { // 无效购买
      this.current = null;
      this.price = null;
      this.isDisabled = false;
    } else { // 有效购买
      this.isDisabled = true;
      this.ticketCardInfo = item;
      this.current = idx;
      this.price = item.price;
      this.playTime = item.extMap.playTime;
      this.position = item.extMap.position;
      this.buyTimesLimit = item.buyTimesLimit; // 限购件数
      this.buyStatus = item.buyStatus;
      this.stock = item.stock;
      if (item.buyTimesLimit === 0) {
        this.maxLength = item.stock;
      }else {
        if (item.stock <= item.buyTimesLimit) {
          this.maxLength = item.stock;
        }else {
          this.maxLength = item.buyTimesLimit;
        }
      }
      this.buttonColor = true; // 是否可以提交，true为真
    }
  }


  /**
   * @description 获取counter组件里面选择的数量
   * @author @zhoukf1349
   * @date 2018-04-08
   * @param obj stock库存
   * @returns {any}
   */
  handleSelectedQuantity(event) {
    if (this.tickets.status === 3) { // 预热中
      this.isDisabled = false;  // 灰色
    }
    if (this.buttonColor === true) {
      this.isDisabled = true;
      if (this.buyTimesLimit === 0) { // 无限购买
        if (event > this.stock) {
          this.tools.showToast(`该商品库存不足.`);
          this.isDisabled = false;
        } else { // 购买数量小于库存数
          this.isDisabled = true;
          this.ticketNum = event;
        }
      } else { // 限购件数
        if (event > this.buyTimesLimit) {
          this.tools.showToast(`该商品最多只能购买${this.buyTimesLimit}件.`);
          this.isDisabled = false;
        } else {
          this.isDisabled = true;
          this.ticketNum = event;
        }
      }
    } else {
      this.isDisabled = false;
    }
  }


// 关闭模态框
  closeModal() {
    this.events.publish('modal:close');
  }

  ngOnInit() {

  }

}
